package com.zhongyu.config;


import com.zhongyu.pojo.Permission;
import com.zhongyu.service.PermissionService;
import com.zhongyu.service.UserService;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Configuration//配置类相当于.xml文件
public class ShiroConfig {

    @Resource
    private UserService userService;
    @Resource
    private PermissionService permissionService;
    /*   @Resource
   private SmbmsRealm smbmsRealm;*/

    @Bean
    public SessionsSecurityManager securityManager(){
        //初始化环境
        DefaultWebSecurityManager securityManager=new DefaultWebSecurityManager();
        securityManager.setRealm(realm());
        securityManager.setCacheManager(ehCacheManager());
        return  securityManager;
    }

    /**
     *
     * 入口类
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
        ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager());
        Map<String,String> filterChains=new HashMap<>();//过滤链
        filterChains.put("/logout","logout");//默认退出会跳转到"/"这个路径
        filterChains.put("/login","anon");//login路径可匿名访问
        filterChains.put("/css/**","anon");
        filterChains.put("/common/**","anon");
        filterChains.put("/js/**","anon");
        filterChains.put("/webjars/**","anon");
        filterChains.put("/**","anon");///**   authc目录及子目录需要认证才能进行访问
        bean.setFilterChainDefinitionMap(filterChains);
        bean.setLoginUrl("/login");//指明登录在什么位置
        return bean;
    }


    @Bean
    public SmbmsRealm realm(){
        SmbmsRealm smbmsRealm=new SmbmsRealm();
        smbmsRealm.setCredentialsMatcher(credentialsMatcher());
  smbmsRealm.setUserService(userService);
  smbmsRealm.setPermissionService(permissionService);
        return  smbmsRealm;
    }
    @Bean
    public HashedCredentialsMatcher credentialsMatcher(){
        HashedCredentialsMatcher matcher=new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");//通过md5加密
        matcher.setHashIterations(1);//加盐次数
        return matcher;
    }

    //开启shiro注解支持
    @Bean
    public  AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(){
        AuthorizationAttributeSourceAdvisor advisor=new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager());
        return advisor;
    }
    @Bean
public EhCacheManager ehCacheManager(){
EhCacheManager ehCacheManager=new EhCacheManager();
ehCacheManager.setCacheManagerConfigFile("classpath:shiro-ehcache.xml");
return ehCacheManager;
}

 /*    @Bean
    public LogoutFilter logout(){//退出
      LogoutFilter logoutFilter=new LogoutFilter();
      logoutFilter.setRedirectUrl("/login");//跳转到登录页
      return logoutFilter;
    }*/
}
